package com.xayy.health.ds.service.impl;

import cn.hutool.core.date.DatePattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xayy.health.ds.model.DsTelemetryData;
import com.xayy.health.mgt.model.vo.PredictDataVo;
import com.xayy.health.mgt.model.vo.TaskResultVo;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xayy.health.ds.model.DsFaultInfo;
import com.xayy.health.ds.service.DsFaultInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsFaultInfoService实现类
 * @Auther:liuyuan
 * @Date:2025-07-09
 */
@Slf4j
@Service
public class DsFaultInfoServiceImpl extends BaseServiceImpl<DsFaultInfo> implements DsFaultInfoService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "TB_FAULT_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }

   @Override
   /**
    * 获取任务预测点集合
    *
    * @param predictDataVo 预测点集合
    * @return List<TaskResultVo>
    */
   public List<TaskResultVo> getTaskTrendDataList(PredictDataVo predictDataVo){
       SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
       String sql = "select time, value\n" +
               "    from HEALTH.tb_calculate_data\n" +
               "    where task_id = " +
               +predictDataVo.getTaskId()+" \n "+
               "    and sate_code = " +
               "'"+predictDataVo.getSatelliteCode()+"'\n"+
               "    and param_code=" +
               "'"+predictDataVo.getParamCode()+"'\n"+
               "    and time  >=  " +
               "'"+sdf.format(predictDataVo.getStartTime())+"'\n"+
               "    and time  <=  " +
               "'"+sdf.format(predictDataVo.getTrendEndTime())+"'\n"+
               "    order by time";
       return selectBySql(sql,TaskResultVo.class);
   }
    @Override
    /**
     * 获取预测段的实时数据
     *
     * @param predictDataVo 实时数据点集合
     * @return List<TelemetryData>
     */
    public List<DsTelemetryData> getRealTimeDataList(PredictDataVo predictDataVo){
        SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        String sql = "select time, value\n" +
                "    from HEALTH.tb_telemetry_data\n" +
                "    where sate_code = " +
                "'"+predictDataVo.getSatelliteCode()+"'\n"+
                "    and param_code=" +
                "'"+predictDataVo.getParamCode()+"'\n"+
                "    and time  >=  " +
                "'"+sdf.format(predictDataVo.getStartTime())+"'\n"+
                "    and time  <=  " +
                "'"+sdf.format(predictDataVo.getTrendEndTime())+"'\n"+
                "    order by time";
        return selectBySql(sql,DsTelemetryData.class);
    }

}
